home *** CD-ROM | disk | FTP | other *** search
- //*************** GLOBALS *****************
-
- var Xara_NEWLINE = (navigator.platform != "Win32") ? "\x0D" : "\x0D\x0A";
- var Xara_FILE_configPath = dw.getConfigurationPath();
- var Xara_FILE_menus = Xara_FILE_configPath + "/Menus/menus.xml";
- var Xara_FILE_menusBackup = Xara_FILE_configPath + "/Menus/menus.xml.xara";
-
-
- function XaraFindMenu(XMLDOM, menuId, menuBarId)
- {
- var retVal = null;
- var menuBarList = XMLDOM.getElementsByTagName("MENUBAR");
- var menuBar = '';
-
- for (var i = 0; i < menuBarList.length; i++)
- {
- if (menuBarList[i].getAttribute("ID") == menuBarId) { menuBar = menuBarList[i]; break; }
- }
-
- if (!menuBar) { return retVal; }
- var menuList = menuBar.getElementsByTagName("MENU");
- for (i = 0; i < menuList.length; i++)
- {
- if (menuList[i].getAttribute("ID") == menuId) { retVal = menuList[i]; break; }
- }
-
- return retVal;
- }
-
- function XaraFindMenuItem(menu, itemId)
- {
- var retVal = null;
- var menuItemList = menu.getElementsByTagName("MENUITEM");
-
- for (i = 0; i < menuItemList.length; i++)
- {
- if (menuItemList[i].getAttribute("ID") == itemId) { retVal = menuItemList[i]; break; }
- }
-
- if (null == retVal)
- {
- var separatorList = menu.getElementsByTagName("SEPARATOR");
- for (i = 0; i < separatorList.length; i++)
- {
- if (separatorList[i].getAttribute("REFERENCES") == itemId) { retVal = separatorList[i]; break; }
- }
- }
-
- return retVal;
- }
-
- function XaraUpdateMenus(XMLDOM, menuOriginal)
- {
- var retVal = true;
-
- // backup menus.xml
- DWfile.write(Xara_FILE_menusBackup, menuOriginal.body.innerHTML);
-
- if (!DWfile.write(Xara_FILE_menus, XMLDOM.body.innerHTML))
- {
- retVal = false;
- alert(MSG_menuFileUpdateFailed.replace(/%s/, MMNotes.localURLToFilePath(FILE_menus)));
- }
-
- return retVal;
- }
-
- function XaraManageMenus()
- {
- var retVal = true;
- var bUpdateMenus = false;
- var bMenuOpFailed = false;
-
- XaraTestFile = Xara_FILE_configPath + XaraTestFile;
-
- // Is the extension installed?
- var bInstalled = DWfile.exists(XaraTestFile);
-
- // Load Menus\menus.xml
- var menuDOM = dw.getDocumentDOM(Xara_FILE_menus);
- var originalMenuDOM = menuDOM;
-
- // Is the menu present?
- if (menuDOM.body.innerHTML.indexOf(XaraMenuId) == (-1))
- {
- // No. Is the extension installed?
- if (bInstalled)
- {
- // Add the insert menu items...
- bMenuOpFailed |= !XaraAddXML(menuDOM, XaraParentMenuId, XaraInsertAfter, XaraMenu, XaraMenuError, "DWMainWindow");
-
- // Add the text context menu item...
- bMenuOpFailed |= !XaraAddXML(menuDOM, XaraContextMenuTextParentId, XaraContextMenuTextInsertAfter, XaraContextMenuText, XaraMenuError, "DWTextContext");
-
- // Add the image context menu item...
- bMenuOpFailed |= !XaraAddXML(menuDOM, XaraContextMenuImageParentId, XaraContextMenuImageInsertAfter, XaraContextMenuImage, XaraMenuError, "DWImageContext");
-
- bUpdateMenus = true;
- }
- }
- else
- {
- // The menu is present - Has the extension been removed?
- if (!bInstalled)
- {
- // Yes, remove the Xara menu entries
- bMenuOpFailed |= !XaraRemoveXML(menuDOM, XaraMenu, XaraMenuError);
-
- // Remove the text context menu item...
- bMenuOpFailed |= !XaraRemoveXML(menuDOM, XaraContextMenuText, XaraMenuError);
-
- // Remove the image context menu item...
- bMenuOpFailed |= !XaraRemoveXML(menuDOM, XaraContextMenuImage, XaraMenuError);
-
- bUpdateMenus = true;
- }
- }
-
- if (bUpdateMenus && !bMenuOpFailed)
- {
- XaraUpdateMenus(menuDOM, originalMenuDOM);
- }
-
- dw.releaseDocument(menuDOM);
-
- return retVal;
- }
-
- function XaraAddXML(XMLDOM, parentMenuId, insertAfterId, menuXML, menuError, menuBarId)
- {
- var retVal = true;
- menuXML = menuXML.replace(/\n/g, Xara_NEWLINE);
-
- if (!XaraAddXMLFunction(XMLDOM, parentMenuId, insertAfterId, menuXML, menuBarId)) { retVal = false; }
- if (!retVal) { alert(menuError.replace(/%s/, "add")); }
- return retVal;
- }
-
- function XaraAddXMLFunction(XMLDOM, parentMenuId, insertAfterId, menuXML, menuBarId)
- {
- var parentMenu = XaraFindMenu(XMLDOM, parentMenuId, menuBarId);
-
- if (null == parentMenu) { return false; }
-
- var insertAfter = XaraFindMenuItem(parentMenu, insertAfterId);
- if (null != insertAfter)
- {
- insertAfter.outerHTML = insertAfter.outerHTML + Xara_NEWLINE + menuXML;
- }
- else
- {
- parentMenu.innerHTML = parentMenu.innerHTML + Xara_NEWLINE + menuXML;
- }
-
- return true;
- }
-
- function XaraRemoveXML(XMLDOM, menuXML, menuError)
- {
- var retVal = true;
-
- // The XML passed in may include any of the following characters which will be parsed incorrectly in a regular expression...
- // Replace these with their literal forms...
- menuXML = menuXML.replace(/\n */g, "\\s*");
- menuXML = menuXML.replace(/\//g, "\\\/");
- menuXML = menuXML.replace(/\"/g, "\\\"");
- menuXML = menuXML.replace(/\./g, "\\.");
- menuXML = menuXML.replace(/\)/g, "\\)");
- menuXML = menuXML.replace(/\(/g, "\\(");
-
- if (!XaraRemoveXMLFunction(XMLDOM, menuXML)) { retVal = false; }
- if (!retVal) { alert(menuError.replace(/%s/, "remove")); }
- return retVal;
- }
-
- function XaraRemoveXMLFunction(XMLDOM, menuXML)
- {
- var retVal = false;
- var xmlDoc = XMLDOM.body.innerHTML;
-
- var matchRE = new RegExp(menuXML, "gi");
- matchRE.multiLine = true;
- var result = matchRE.exec(xmlDoc);
-
- if (result != null)
- {
- var startIndex = result.index;
- var stopIndex = matchRE.lastIndex;
-
- if (startIndex > (-1))
- {
- XMLDOM.body.innerHTML = RegExp.leftContext + RegExp.rightContext;
- retVal = true;
- }
- }
-
- return retVal;
- }